unit clsCarta;

interface

uses dm,dbtables,biblio;

type
  TCarta=Class(TObject)
    public
      Texto: string;
      function  Recuperar(Cod_carta: string): boolean;
      function  Retorna_Ultima : string;
      function  Retorna_Primeira : string;
      procedure Gravar (Cod_carta: String);
      procedure Excluir(Cod_carta: String);
      procedure Consultar(var tq: TQuery);
    end;

implementation

procedure TCarta.Gravar;
var tq: TQuery;
begin
 tq             := Tquery.Create(DmInt);
 tq.databasename:= dmint.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select * from carta where Cod_carta=:a');
 tq.params[0].asstring:= Cod_Carta;
 tq.open;
 if tq.eof then begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Insert into Carta values ('+
             ':k_Cod_Carta             ,'+
             ':k_Texto                    )');
 end else begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Update Carta set '+
             'Texto=:k_Texto '+
             'where Cod_Carta=:k_Cod_Carta');
 end;
 tq.parambyname('k_Cod_Carta').asstring:= Cod_Carta;
 tq.parambyname('k_Texto').asblob         := Texto;
 tq.execsql;
 tq.close;
 tq.Free;
end;

procedure TCarta.Excluir;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Delete from Carta where Cod_Carta=:a');
 tq.params[0].asstring:= Cod_Carta;
 tq.execsql;
 tq.close;
 tq.Free;
end;

function TCarta.Recuperar;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select * from Carta where Cod_Carta=:a');
 tq.params[0].asstring:= Cod_Carta;
 tq.open;
 if tq.eof then result:= false else result:= true;
 Texto:= tq.fieldbyname('Texto').asstring;
 tq.close;
 tq.free;
end;

function TCarta.Retorna_Ultima;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select max(Cod_Carta) ultimo from Carta');
 tq.open;
 if tq.fieldbyname('ultimo').asstring='' then
  result:= '001'
 else
  result:= strzero(tq.fieldbyname('ultimo').asinteger+1,3);
 tq.close;
 tq.Free;
end;

procedure TCarta.Consultar;
begin
 tq.close;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select * from Carta order by Cod_Carta');
 tq.open;
end;

function TCarta.Retorna_Primeira: string;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select min(Cod_Carta) pri from Carta');
 tq.open;
 if tq.fieldbyname('pri').asstring<>'' then
  result:= tq.fieldbyname('pri').AsString
 else
  result:= '';
 tq.close;
 tq.Free;

end;

end.
